package com.itmd.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itmd.domain.User;
import com.itmd.domain.VO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping(value="/quick",method = RequestMethod.GET,params = {"userName"})
    public String save() {
        System.out.println("UserController saving running...");
        return "success";
    }


    @RequestMapping("/quick2")
    public ModelAndView save2() {

        ModelAndView  modelAndView = new ModelAndView();
        //添加页面数据
        modelAndView.addObject("userName","EastM");
        //设置返回的页面 view名称
        modelAndView.setViewName("success");
        return modelAndView;

    }

    @RequestMapping("/quick3")
    public ModelAndView save3(ModelAndView modelAndView) {
        modelAndView.addObject("userName","FrontCoder_EastM");
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping("quick4")
    public String save4(Model model) {
        model.addAttribute("userName","BackEndCoder_EastM");
        return "success";
    }

    @RequestMapping("/quick5")
    public String save5(HttpServletRequest request) {
        request.setAttribute("userName","FullStack_EastM");
        return "success";
    }
    @RequestMapping("/quick6")
    public void save6(HttpServletResponse response) throws IOException {
        response.getWriter().print("hello world");
    }

    @RequestMapping("quick7")
    @ResponseBody //告知springMVC  返回数据到response body 内。而不是直接返回到视图
    public String save7() {
        return "Hello md";
    }

    @RequestMapping("/quick8")
    @ResponseBody
    public String save8() throws JsonProcessingException {
        User user = new User();
        user.setAge(18);
        user.setUserName("helloName");
        ObjectMapper objectMapper = new ObjectMapper();
//        使用json的转换工具将对象转换为json字符串
        String json =  objectMapper.writeValueAsString(user);
        return json;
    }

    @RequestMapping("/quick9")
    @ResponseBody
    public User save9() throws JsonProcessingException {
        User user = new User();
        user.setAge(30);
        user.setUserName("helloName");
        return user;
    }

    @RequestMapping("/quick10")
    @ResponseBody
    public void save10(String userName,int age) {
        System.out.println(userName);
        System.out.println(age);
    }


//    POJO类型参数
    @RequestMapping("quick11")
    @ResponseBody
    public void save11(User user) {
        System.out.println(user.getUserName());
        System.out.println(user.getAge());
    }

    //数组类型参数
    @RequestMapping("/quick12")
    @ResponseBody
    public void save12(String[] strs) {
        System.out.println(Arrays.asList(strs));
    }

    //数组类型参数,单个字符串
    @RequestMapping("/quick13")
    @ResponseBody
    public void save13(String[] strs) {
        System.out.println(Arrays.asList(strs));
    }

    //集合类型 List<User>
    @RequestMapping("/quick14")
    @ResponseBody
    public void save14(VO vo){
        System.out.println(vo);
    }

    //集合类型 List<User>,不封装成VO，直接接受
    @RequestMapping(value = "/quick15",method = RequestMethod.POST)
    @ResponseBody
    public User save15(@RequestBody List<User> userList) {
        System.out.println(userList);
        return userList.get(0);
    }


    @RequestMapping("/quick16")
    @ResponseBody
    public void save16(@RequestParam(value = "name",required = true, defaultValue = "哈哈") String userName) {
        System.out.println(userName);
    }

//    Restful风格的参数请求
    @RequestMapping(value = "/quick17/{name}",method = RequestMethod.GET)
    @ResponseBody
    public void save17(@PathVariable(value = "name",required = true) String name) {
        System.out.println("name::" + name);
    }

    @RequestMapping("/quick18")
    @ResponseBody
    public void save18(String userName) {
        System.out.println(userName);
    }

    @RequestMapping("/quick19")
    @ResponseBody
    public void save19(Date date) {
        System.out.println(date);
    }

    @RequestMapping("/quick20")
    @ResponseBody
    public void save20(String username, MultipartFile uploadFile) {
        System.out.println(username);
//        System.out.println(uploadFile);
//        获取文件上传的名称
        String origiName = uploadFile.getOriginalFilename();
        try {
            uploadFile.transferTo(new File("/Users/eastm/Desktop/upload/"+origiName));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/quick21")
    @ResponseBody
    public void save21(String username, MultipartFile[] uploadFiles) {
        System.out.println(username);
//        System.out.println(uploadFile);
//        获取文件上传的名称
//        String origiName = uploadFile.getOriginalFilename();
//        try {
//            uploadFile.transferTo(new File("/Users/eastm/Desktop/upload/"+origiName));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        for(MultipartFile file: uploadFiles) {
            String origiName = file.getOriginalFilename();
            try {
                file.transferTo(new File("/Users/eastm/Desktop/upload/"+origiName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
